package com.ruoyi.cms.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.cms.entity.domain.Model;
import com.ruoyi.cms.entity.domain.ModelParameter;
import com.ruoyi.cms.mapper.ModelMapper;
import com.ruoyi.cms.service.ModelService;
import com.ruoyi.common.context.AppUserContext;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.RedisKeyEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.ModelParameterMapper;
import com.ruoyi.cms.service.ModelParameterService;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 模型参数Service业务层处理
 */
@Service
@Slf4j
public class ModelParameterServiceImpl implements ModelParameterService {

    @Autowired
    private ModelParameterMapper modelParameterMapper;
    @Autowired
    private ModelService modelService;
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public ModelParameter get(Long modelId) {
        ModelParameter modelParameter = redisCache.getCacheObject(RedisKeyEnum.CMS_MODEL_PARAMETER.getPrefix() + modelId);

        if (ObjUtil.isNull(modelParameter)) {
            LambdaQueryWrapper<ModelParameter> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ModelParameter::getModelId, modelId);
            queryWrapper.last("limit 1");
            modelParameter = modelParameterMapper.selectOne(queryWrapper);

            if (ObjUtil.isNull(modelParameter)) {
                modelParameter = new ModelParameter();
                modelParameter.setModelId(modelId);
                modelParameter.setUpdateTime(new Date());
                if(ObjUtil.isNotNull(AppUserContext.getUser())){
                    modelParameter.setUpdater(AppUserContext.getUser().getUserId());
                }

                modelParameterMapper.insert(modelParameter);
            }

            setRedis(modelId, modelParameter);
        }

        return modelParameter;
    }

    @Override
    public R<?> update(ModelParameter modelParameter, Boolean auth) {
        if (auth) {
            LambdaQueryWrapper<Model> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Model::getId, modelParameter.getModelId());
            queryWrapper.last("limit 1");
            Model model = modelMapper.selectOne(queryWrapper);

            modelService.modelCheck(model);

            modelParameter.setUpdater(AppUserContext.getUser().getUserId());
        }

        modelParameter.setUpdateTime(new Date());
        modelParameterMapper.updateById(modelParameter);

        setRedis(modelParameter.getModelId(), modelParameter);

        return R.ok();
    }

    private void setRedis(Long modelId, ModelParameter modelParameter) {
        redisCache.setCacheObject(RedisKeyEnum.CMS_MODEL_PARAMETER.getPrefix() + modelId,
                modelParameter,
                RedisKeyEnum.CMS_MODEL_PARAMETER.getTimeout(),
                RedisKeyEnum.CMS_MODEL_PARAMETER.getTimeUnit());
    }


}
